package cn.shiro.shiroservice.moudle.db.service.impl;

import cn.shiro.shiroservice.moudle.db.pojo.dto.DataSourceDTO;
import cn.shiro.shiroservice.moudle.db.service.SimpleCache;
import io.lettuce.core.RedisException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * &#064;Time 2024 一月 星期一 17:43
 *
 * @author ShangGuan
 */
@Service
public class SimpleCacheImpl implements SimpleCache<DataSourceDTO> {

    private final RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    public SimpleCacheImpl(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void setCache(DataSourceDTO value) {
        try {
            redisTemplate.opsForValue().set("",value);
        }catch (RedisException redisException){
            throw new RuntimeException("redis缓存异常");
        }
    }

    @Override
    public DataSourceDTO getCache(String key) {
        return (DataSourceDTO) redisTemplate.opsForValue().get(key);
    }

    @Override
    public void delete(String key) {
        try {
            redisTemplate.delete(key);
        }catch (RedisException redisException){
            throw new RuntimeException("redis缓存异常");
        }
    }

    @Override
    public void update(String key, DataSourceDTO value) {
        try {
            redisTemplate.opsForValue().set(key,value);
        }catch (RedisException redisException){
            throw new RuntimeException("redis缓存异常");
        }
    }
}
